%m1 = [2.79 2.44 1 1 1 1 1 1 1 1 1 1];
%THIS IS A COMMENT
%another
%m2 = [-0.28 4.54 1 1 1 1 1 1 1 1 1 1];
%m3 = [m1 ; m2];
clear;
%mix = [2 4 8 16];
mix = [8];
%iter = [2 3 4 5 6 7 8 9 10];
iter = [10];
%iter = [2]
lowest_freq =34193;
%all_classes = {'phonemes/nasals.dat.txt'};
%all_classes = {'phonemes/stops.dat.txt'};
%all_classes  = {'phonemes/affricates.dat.txt','phonemes/fricatives.dat.txt','phonemes/vowels.dat.txt'};
%all_classes  = {'phonemes/affricates.dat.txt','phonemes/fricatives.dat.txt','phonemes/liquids_glides.dat.txt',...
%    'phonemes/nasals.dat.txt','phonemes/stops.dat.txt','phonemes/vowels.dat.txt'}

classes(1).classname= 'phonemes/affricates.dat.txt'
classes(1).freq = 34193;
classes(2).classname= 'phonemes/fricatives.dat.txt'
classes(2).freq = 458031;
classes(3).classname= 'phonemes/liquids_glides.dat.txt'
classes(3).freq = 257248;
classes(4).classname= 'phonemes/nasals.dat.txt'
classes(4).freq = 186139;
classes(5).classname= 'phonemes/stops.dat.txt'
classes(5).freq = 446891;
classes(6).classname= 'phonemes/vowels.dat.txt'
classes(6).freq = 983231;

for p = 1:size(classes,2)
    path = classes(p).classname;
    class_freq = classes(p).freq;
    [phn_class] = textread(char(path),'%s');
    combo = 0;
    result_path = char(strcat('results_freq_normalized/','result_freq_normalized_',strrep(path,'phonemes/','')))
    file_1 = fopen(result_path,'w');
    %frames_path = char(strcat('mfcc_per_phone_class/frames_',strrep(path,'phonemes/','')))
    %file_2 = fopen(frames_path,'w');
    for m = 1:size(mix,2)
        for it = 1:size(iter,2) 
            gmm_mixtures =mix(m);
            iterations = iter(it);

            initial_means = ones(gmm_mixtures,12);
            %initial_means = (rand(4,12)  - 0.5) .* 4;
            initial_variance = ones(gmm_mixtures,12);
            initial_weights = ones(gmm_mixtures,1) * (1/gmm_mixtures);

            user(1000).name = '';
            user(1000).mean = [];
            user(1000).variance = [];
            user(1000).weights = [];
            user(1000).mean_phn_class = [];
            user(1000).variance_phn_class = [];
            user(1000).weights_phn_class = [];

            test_user(1000).name = '';
            test_user(1000).test_vectors = cell(1,100);

            folders = pathlist(genpath('TIMIT/TRAIN/D2'));
            count_users = 0;
            
            cumulative_phn_class_mfcc = 0;
            cumulative_mfcc = 0;
            
            for f = 1:size(folders,1)
               folder = folders(f);
               folder = folder
               wavs = dir(strcat(char(folder),'/*.WAV'));

               if (size(wavs,1) > 0)
                folder = folder;
                count_users = count_users + 1;
                user(count_users).name = char(folder);
                test_user(count_users).name = char(folder);

                audio_data = [];
                st_data = [];
                en_data=[];
                phoneme_data = [];
                Fs = [];

                %first 20 percent of wav files set aside for testing

                set_aside_for_test = size(wavs,1) * 0.2;

                for t_w = 1:uint8(set_aside_for_test)
                   t_w = t_w;
                   [raw_data_test Fs WRD PHN] =  readsph(strcat(char(folder),'/',wavs(t_w).name));
                   disp('testing:');
                   wavfile = strcat(char(folder),'/',wavs(t_w).name)
                   phnfile = strrep(wavfile,'.WAV','.PHN');
                   [st en phonemes_of_test_file] = textread(phnfile,'%d %d %s');
                   mfcc_of_test_file = melcepst(raw_data_test,8000);
                   phn_class_index_of_test_file = find(ismember(phonemes_of_test_file,phn_class) == 1);
                   phn_class_mfcc_of_test_file = (SubSample(mfcc_of_test_file,st(phn_class_index_of_test_file) ./ 64,en(phn_class_index_of_test_file) ./ 64))';
                  
                   if( isequal(size(phn_class_mfcc_of_test_file),[0 0]))
                       test_user(count_users).test_vectors{t_w} = mfcc_of_test_file;
                   else
                       test_user(count_users).test_vectors{t_w} = phn_class_mfcc_of_test_file';
                   end
                   
                end

                %remaining wav files used for training
                 
                for w = uint8(set_aside_for_test)+1:size(wavs,1)
                    w =w;
                    [raw_data Fs] =  readsph(strcat(char(folder),'/',wavs(w).name));
                    disp('training:');
                    wavfile = strcat(char(folder),'/',wavs(w).name)
                    phnfile = strrep(wavfile,'.WAV','.PHN');
                    [st en phonemes] = textread(phnfile,'%d %d %s');
                    st_data = [st_data; st + size(audio_data,1)];
                    en_data = [en_data; en + size(audio_data,1)];
                    phoneme_data = [phoneme_data; phonemes];
                    %mfc = size(mfcc_audio_data)
                    %audio = size(audio_data)
                    audio_data = [audio_data;  raw_data];
                end
                   %sub_sample_audio_data =  
                   %size(st_data);
                   %size(en_data);
                   %size(phoneme_data);
                   phn_class_index = find(ismember(phoneme_data,phn_class) == 1);
                   %ae_audio_data = SubSample(audio_data,st_data(ae_index),en_data(ae_index));

                  
                   %can create different mfcc data (read in the folders above,
                   %outside of all loops) ... list of phn_classs, etc. subsample
                   %will be for all the phn_classs, or whatever. wherever ae_index
                   %occurs, this will be replaced by whatever you're looking
                   %for. If there's more than one phoneme
                   
                   mfcc_audio_data = melcepst(audio_data,8000);
                   phn_class_mfcc_audio_data = (SubSample(mfcc_audio_data,st_data(phn_class_index) ./ 64,en_data(phn_class_index) ./ 64))';
                   cumulative_phn_class_mfcc = size(phn_class_mfcc_audio_data,2) + cumulative_phn_class_mfcc;
                   cumulative_mfcc = size(mfcc_audio_data,1) + cumulative_mfcc;
                   
                   %FUTHER SUBSAMPLING SO THAT ALL PHONE CLASSES USE THE
                   %SAME NUMBER OF MFCCS IN TRAINING
                   sub_ratio = lowest_freq/class_freq;
                   s = randsample(1:size(phn_class_mfcc_audio_data,2), floor(size(phn_class_mfcc_audio_data,2) * sub_ratio));
                   phn_class_mfcc_audio_data = phn_class_mfcc_audio_data(:,s);
                   %%mfcc = size(mfcc_audio_data)
                   %%audio = size(audio_data)
                   %%rand_index = uint32(rand(gmm_mixtures,1) * size(mfcc_audio_data,1));
                   %%rand_index(rand_index == 0) = [1];

                   %rand_index = rand_index
                   %%initial_means = mfcc_audio_data(rand_index',:);
                   %gaussmix will apply to whatever you're looking for,
                   %ae_mfcc, etc. 
                   %%[gmm_mu,gmm_sigma,gmm_weights] = gaussmix(mfcc_audio_data,[], iterations,initial_means, initial_variance, initial_weights);
                   %%user(count_users).mean = gmm_mu;
                   %%user(count_users).variance = gmm_sigma;
                   %%user(count_users).weights = gmm_weights;
                    
                   %rand_index = 1:uint32(size(phn_class_mfcc_audio_data,2)/gmm_mixtures):size(phn_class_mfcc_audio_data,2)
                   %rand_index(gmm_mixtures+1:size(rand_index,2)) = []
                   %rand_index(rand_index == 0) = [1]
                   if (isequal ( size(phn_class_mfcc_audio_data),[0 0]))
                       
                   else
                       size_of_phone_class_mfcc  = size(phn_class_mfcc_audio_data)
                       rand_index =  randsample(size(phn_class_mfcc_audio_data,2),int8(gmm_mixtures))%uint32(rand(gmm_mixtures,1) * size(phn_class_mfcc_audio_data,1));
                       
                       phn_class_mfcc_audio_data = phn_class_mfcc_audio_data';
                       initial_means = phn_class_mfcc_audio_data(rand_index',:);
                       size_vmfcc = size(phn_class_mfcc_audio_data);
                       size_iter = size(iterations);
                       size_mean = size(initial_means);
                       size_var = size(initial_variance);
                       size_weight = size(initial_weights);
                       [gmm_mu_phn_class,gmm_sigma_phn_class,gmm_weights_phn_class] = gaussmix(phn_class_mfcc_audio_data,[], iterations,initial_means, initial_variance, initial_weights);
                       user(count_users).mean_phn_class = gmm_mu_phn_class;
                       user(count_users).variance_phn_class = gmm_sigma_phn_class;
                       user(count_users).weights_phn_class = gmm_weights_phn_class;
                   end

               end

            end
            
            %fprintf(file_2,'%d\t%d\t%d\t%d\n',cumulative_phn_class_mfcc,cumulative_mfcc, uint8(gmm_mixtures), uint8(iterations));
            
            user(count_users+1:1000) = [];

            %rand_index(gmm_mixtures+1:size(rand_index,1)) = [];

            user = user

            test_user(count_users+1:1000) = [];
            test_user = test_user


            count_of_total_test_vectors = 0;
            count_of_correct_test_vectors = 0;
            for t = 1:size(test_user,2)

                disp(sprintf(' testing %d of %d ', t, count_users));
                test_vectors = test_user(t).test_vectors;
                for t_v = 1:size(test_user(t).test_vectors,2)
                    test_mfcc = test_user(t).test_vectors{t_v};
                    max_mean_lp_user = [];
                    max_mean_lp = -Inf;
                    for u = 1:size(user,2)
                        if (isequal(user(u).mean_phn_class,[]))
                            
                        else
                            [lp rp kh kp] = gaussmixp(test_mfcc,user(u).mean_phn_class, user(u).variance_phn_class, user(u).weights_phn_class);
                            mean_lp = mean (lp);
                            if(mean_lp > max_mean_lp)
                                max_mean_lp = mean_lp;
                                max_mean_lp_user = user(u);
                            end
                        end
                       
                    end
                    count_of_total_test_vectors = count_of_total_test_vectors + 1;
                    if( strcmp(test_user(t).name,max_mean_lp_user.name) == 1)
                        count_of_correct_test_vectors = count_of_correct_test_vectors +1;
                    end
                end
            end
            count_of_correct_test_vectors = count_of_correct_test_vectors
            count_of_total_test_vectors = count_of_total_test_vectors
            combo  = combo + 1;
            correct = count_of_correct_test_vectors/count_of_total_test_vectors;
            %accuracy_report{combo} = sprintf(' mixtures = %d iterations = %d accuracy = %f ', gmm_mixtures, iterations,count_of_correct_test_vectors/ count_of_total_test_vectors)
            fprintf(file_1,'%d\t%d\t%f\n',gmm_mixtures,iterations, correct);
        end
    end
    fclose(file_1)
end





  
    
  




